package com.powernode.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.domain.HotSearch;
import com.powernode.mapper.HotSearchMapper;
import com.powernode.service.HotSearchService;
import com.powernode.utils.AuthUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 内容:
 * 作者: cy
 */
@Service
@Slf4j
public class HotSearchServiceImpl extends ServiceImpl<HotSearchMapper, HotSearch> implements HotSearchService {

    @Autowired
    private HotSearchMapper hotSearchMapper;

    /**
     * 分页查询热搜
     * @param page
     * @param hotSearch
     * @return
     */
    @Override
    public Page<HotSearch> findHotSearchPage(Page<HotSearch> page, HotSearch hotSearch) {
        return hotSearchMapper.selectPage(page, new LambdaQueryWrapper<HotSearch>()
                .eq(!ObjectUtils.isEmpty(hotSearch.getStatus()), HotSearch::getStatus, hotSearch.getStatus())
                .like(StringUtils.hasText(hotSearch.getTitle()), HotSearch::getTitle, hotSearch.getTitle())
                .like(StringUtils.hasText(hotSearch.getContent()), HotSearch::getContent, hotSearch.getContent())
                .orderByDesc(HotSearch::getSeq)
        );
    }

    /**
     * 新增热搜
     * @param hotSearch
     */
    @Override
    public void addHotSearch(HotSearch hotSearch) {
        Long userId = AuthUtil.getUserIdOrShopId(true);
        Long shopId = AuthUtil.getUserIdOrShopId(false);
        log.info("新增热搜{}，用户id为{}，店铺id为{}", JSON.toJSONString(hotSearch), userId, shopId);
        hotSearch.setRecDate(new Date());
        hotSearchMapper.insert(hotSearch);
    }
}
